#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_SIZE 10
#define Null -1
typedef int Tree;
typedef char Element_type;
struct BinaryTree
{
    Element_type Element;
    Tree Left;
    Tree Right;
}T1[MAX_SIZE], T2[MAX_SIZE];

Tree BuildTree(struct BinaryTree T[]);
bool Isomorphic(Tree R1, Tree R2);

int main()
{
    Tree R1, R2;
    R1 = BuildTree(T1);
    R2 = BuildTree(T2);
    if(Isomorphic(R1,R2))
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}

Tree BuildTree(struct BinaryTree T[])
{
    int i, N, check[MAX_SIZE], Root=Null;
    Element_type cl, cr;
    scanf("%d\n", &N);
    for (i = 0; i < N; i++)
        check[i] = 0;
    if(N)
    {    
        for (i = 0; i < N;i++)
        {
            scanf("%c %c %c\n", &T[i].Element, &cl, &cr);
            if(cl!='-')
            {
                T[i].Left = cl - '0';
                check[T[i].Left] = 1;
            }
            else
                T[i].Left = Null;
            if(cr!='-')
            {
                T[i].Right = cr - '0';
                check[T[i].Right] = 1;
            }
            else
                T[i].Right = Null;
            
        }
        for (i = 0; i < N; i++)
            if(!check[i])
                break;
        Root = i;
    }

    return Root;
}

bool Isomorphic(Tree R1, Tree R2)
{
    if(R1==Null && R2==Null)
        return true;
    else if((R1==Null && R2!=Null) || (R1!=Null && R2==Null))
        return false;
    else if(T1[R1].Element != T2[R2].Element)
        return false;
    else if (T1[R1].Left == Null && T2[R2].Left == Null)
        return Isomorphic(T1[R1].Right, T2[R2].Right);

    if ((T1[R1].Left != Null && T2[R2].Left != Null) && (T1[T1[R1].Left].Element == T2[T2[R2].Left].Element))
        return Isomorphic(T1[R1].Left, T2[R2].Left) && Isomorphic(T1[R1].Right, T2[R2].Right);
    else
        return Isomorphic(T1[R1].Left, T2[R2].Right) && Isomorphic(T1[R1].Right, T2[R2].Left);
}